package com.fmi.chesstern.gameboard;

import java.util.HashMap;
import java.util.Map;

import com.fmi.chesstern.pieces.ChessPiece;

public class GameBoard {
	
	private GameField[][] fields = null;
	private Map<String, ChessPiece> pieces = null;
	
	public boolean addPiece(ChessPiece piece, GameField field) {
		boolean result = false;
		if (pieces != null) {
			if (pieces.get(field.toString()) == null) {
				pieces.put(field.toString(), piece);
				result = true;
			}
		}
		return result;
	}
	
	public boolean removePiece(GameField field) {
		boolean result = false;
		if (pieces != null) {
			pieces.remove(field.toString());
			result = true;
		}
		return result;
	}
	
	public void movePiece(GameField start, GameField goal) {
		removePiece(goal);
		addPiece(pieces.get(start.toString()), goal);
		removePiece(start);
	}
	
	public Map<String, ChessPiece> getPieces() {
		return pieces;
	}

	public void setPieces(Map<String, ChessPiece> pieces) {
		this.pieces = pieces;
	}

	public GameField[][] getFields() {
		return fields;
	}

	public void setFields(GameField[][] flds) {
		this.fields = flds;
	}

	public GameBoard() {
		initBoard();
	}
	
	public GameBoard(GameField[][] flds, Map<String, ChessPiece> pcs) {
		this.fields = flds;
		this.pieces = pcs;
	}
	
	public GameBoard(GameBoard other) {
		pieces = new HashMap<String, ChessPiece>(other.getPieces()); 
		fields = other.getFields();
	}
	
	public void clearBoard() {
		fields = new GameField[8][8];
		pieces = new HashMap<String, ChessPiece>();
	}
	
	public void initBoard() {
		pieces = new HashMap<String, ChessPiece>(); 
		fields = new GameField[8][8];
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				fields[i][j] = new GameField(i, j);
				pieces.put(fields[i][j].toString(), null);
			}
		}
	}
}
